<template>
	<view>
		<FullscreenLoading :loading="loading" :config="minishop.config"></FullscreenLoading>
		<view class="muu-minishop" :class="[minishop.config.style]" v-if="!loading">
			<view class="main-section tabbar-bottom">
				<view class="item" v-for="(item,index) in pageData.data" :key="index">
					<view v-if="item.type==='announce'">
						<Announce title="公告组件" type="announce" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='category'">
						<Category title="分类组件" type="category" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='category_nav'">
						<CategoryNav title="图文导航组件" type="category_nav" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='custom_text'">
						<CustomText title="自定义文本组件" type="custom_text" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='search'">
						<Search title="搜索组件" type="search" :config="minishop.config" />
					</view>
					<view v-if="item.type==='slideshow'">
						<SlideShow title="轮播组件" type="slideshow" :slideshowStyle="Number(item.style)" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='single_img'">
						<SingleImg title="单图广告组件" type="single_img" :singleImgStyle="Number(item.style)" :params="item.data" :config="minishop.config"/>
					</view>
					<view v-if="item.type==='weixin'">
						<Weixin title="关注公众号组件" type="weixin" :thisStyle="Number(item.style)" :config="minishop.config" />
					</view>
					<view v-if="item.type==='goods_list'">
						<MinishopGoodsList title="云小店商品列表组件" type="minishop_goods_list" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='activity_list'">
						<ActivityList title="线下活动列表组件" type="activity_list" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='scoreshop_goods_list'">
						<ScoreshopGoodsList title="积分商城商品列表组件" type="scoreshop_goods_list" :params="item.data" :config="minishop.config" />
					</view>
					<view v-if="item.type==='miniprogram_live_list'">
						<MiniprogramLiveList title="小直播列表组件" type="miniprogram_live_list" :params="item.data" :config="minishop.config" />
					</view>
				</view>
				
				<template v-if="!loading">
					<CopyRight :data="minishop.config.config.copyright" />
				</template>
			</view>
			<tabBar :config="minishop.config" />
		</view>
	</view>
	
</template>

<script>
	import Base from '@/muu_minishop/common/base.vue'
	//DIY组件 按首字母顺序排列
	import ActivityList from '@/common/diys/ActivityList/ActivityList.vue' //线下活动组件
	import Announce from '@/common/diys/Announce/Announce.vue' //公告组件
	import Category from '@/common/diys/Category/Category.vue' //分类组件
	import CategoryNav from '@/common/diys/CategoryNav/CategoryNav.vue' //图文导航组件
	import CustomText from '@/common/diys/CustomText/CustomText.vue' //自定义文本组件
	import MinishopGoodsList from '@/common/diys/MinishopGoodsList/MinishopGoodsList.vue' //云小店商品列表组件
	import MiniprogramLiveList from '@/common/diys/MiniprogramLiveList/MiniprogramLiveList.vue' //小直播列表组件
	import ScoreshopGoodsList from '@/common/diys/ScoreshopGoodsList/ScoreshopGoodsList.vue' //积分商城商品列表组件
	import Search from '@/common/diys/Search/Search.vue' //搜索组件
	import SlideShow from '@/common/diys/SlideShow/SlideShow.vue' //轮播组件
	import SingleImg from '@/common/diys/SingleImg/SingleImg.vue' //单图广告组件
	import Weixin from '@/common/diys/Weixin/Weixin.vue' //关注公众号组件
	//公共组件
	import CopyRight from '@/common/components/copyright.vue' //自定义版权
	import TabBar from "../common/tabBar.vue" //底部导航

	export default {
		extends:Base,
		name: 'index',
		components: {
			ActivityList,
			Announce,
			Category,
			CategoryNav,
			CustomText,
			MiniprogramLiveList,
			MinishopGoodsList,
			ScoreshopGoodsList,
			Search,
			SlideShow,
			SingleImg,
			Weixin,
			CopyRight,
			TabBar,
		},
		data() {
			return {
				userInfo : {}, //用户数据
				pageData : [], //首页数据
			}
		},

		onPullDownRefresh() {
			console.log('refresh');
			uni.removeStorageSync('muu_minishop_config')
			uni.removeStorageSync('muu_minishop_page_data')
			// 重新获取页面数据
			this.getApiPageData()
			// 更新全局配置
			this.$store.dispatch('minishop/getConfig').then(res => {
				console.log(res)
			})
		},
		async onLoad(option) {
			await this.$onLaunched;
			await this.baseIsReady()
			
			//#ifdef MP-WEIXIN
			//通过小程序码进入的获取参数
			if(option.scene){
				// options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
				let scene = decodeURIComponent(option.scene); 
				let obj = {};
				for (var i = 0; i < scene.split('*').length;i++){
				  var arr = scene.split('*')[i].split('/');
				  obj[arr[0]] = arr[1];
				}
				this.urlParams.id = obj.id
				this.urlParams.promoter_uid = obj.u
			}
			//#endif
			
			await this.getPageData()
			await this.getUserInfo()
			await this.initPage()
			await this.$nextTick(() => {
				this.loading = false
			})
		},
		created() {
			this.$store.commit('setPageIndex',0)
		},
		
		methods: {
			
			async initPage() {
				
				//设置原生导航栏
				//#ifdef MP
				this.setNavigation(this.minishop.config.style);
				//#endif
				
				let title = '云小店'
				if(this.minishop.config.title !== undefined){
					title = this.minishop.config.title
				}
				setTimeout(()=>{
					//设置原生导航栏标题
					uni.setNavigationBarTitle({
					　　title: title
					})
				},200)
				
				if(this.urlParams.promoter_uid){
					uni.setStorageSync('promoter_uid',this.urlParams.promoter_uid)
				}
				
				// H5端分享设置
				//#ifdef H5
				if(this.minishop.config.config.share.title !== undefined){
					let url = window.location.href
					let memberInfo
					if(this.userInfo){
						memberInfo = this.userInfo.memberInfo || {}
					}
					if(memberInfo && memberInfo.uid !== undefined){
						url = url + '?promoter_uid=' + memberInfo.uid
					}
					let shareData = {
						title : this.minishop.config.config.share.title,
						desc : this.minishop.config.config.share.desc,
						img: this.minishop.config.config.share.icon
					}
					this.wechat.share(shareData,url);
				}
				//#endif
			},
			
			/**
			 * 获取页面数据
			 */
			async getPageData(){
				let that = this
				return new Promise((resolve, reject) => {
					const page_data = uni.getStorageSync('muu_minishop_page_data');
					if(page_data.deadtime){
						let newtime = Date.parse(new Date());
						if(page_data.deadtime < newtime){
							resolve(that.getApiPageData())
						}else{
							that.pageData = page_data.data
							resolve(page_data)
						}
					}else{
						resolve(that.getApiPageData())
					}
				})
			},
			
			/**
			 * 获取接口数据
			 */
			getApiPageData: function(){
				let that = this
				
				let params = this.urlParams
				//console.log(params)
				let data = {}
					data.type = 1
					if(params.promoter_uid !== undefined){
						data.promoter_uid = params.promoter_uid
					}
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/index'
					//#endif
					//#ifdef H5
					url = 'entry/site/index'
					//#endif
				//获取首页数据
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.pageData = res.data.data
							// 缓存试卷 一个小时
							let deadtime = Date.parse(new Date()) + 3600 * 1000
							let page_data_store = {
								'deadtime' : deadtime,
								'data' : res.data.data
							}
							//设置本地缓存
							uni.setStorageSync('muu_minishop_page_data',page_data_store);
							//结束下拉刷新
							uni.stopPullDownRefresh();
						}
					},
				})
			},
			
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				return new Promise((resolve, reject) => {
					let userInfo = uni.getStorageSync('userInfo') || {}
					if(userInfo){
						this.userInfo = userInfo
						resolve(userInfo)
					}else{
						this.userInfo = {}
						resolve(res)
					}
				})
			},
			
			setNavigation: function(style) {
				let that = this
				//动态更改顶部状态栏演示
				let backgroundColor = '#ffffff'
				//console.log(this.config)
				if(style == 'Green'){
					backgroundColor = '#00ce74';
				}
				if(style == 'Blue'){
					backgroundColor = '#03b8cf';
				}
				if(style == 'LightRed'){
					backgroundColor = '#ea644a';
				}
				if(style == 'Orange'){
					backgroundColor = '#ff9900';
				}
				if(style == 'LightPink'){
					backgroundColor = '#ffb6c1';
				}
				if(style == 'Magenta'){
					backgroundColor = '#8666b8';
				}
				console.log(backgroundColor);
				//更改小程序原生导航栏
				uni.setNavigationBarColor({
					frontColor: '#ffffff',
					backgroundColor: backgroundColor,
				})
			},
			isEmptyObject(obj) {
				for(var key in obj){
					break;  return false
				};
				return true  
			},
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {
			// 分享(分销)用户ID
			let promoterUid = this.userInfo.memberInfo.uid || 0
			let share = {
				title : this.config.title,
				path : '/muu_minishop/pages/index/index?' + '&promoter_uid=' + promoterUid,
				desc : this.config.description,
				success : function(){
					
				},
			}
			
			return share
		},
		/**
		 * 监听用户点击右上角转发到朋友圈
		 */
		onShareTimeline() {
			
		}
	}
</script>

<style lang="scss">

</style>
